<?php

function add_model() {
	
	include_once libfile('function/pinyin');
	$table = pinyin($_POST['mname']);

	$mtable = DB::getOne("SELECT mtable FROM ".DB::table('model')." WHERE mtable='$table'");

	if($mtable) {
		cpmsg('model_exists', 'action=model&operation=list', 'error');
	}
	
	drop_table($table);
	drop_table($table.'cat');
	drop_table($table.'attach');
	DB::delete('model', "mtable='$table'");
	
	create_modeltable($table);
	create_modelcat($table.'cat');
	create_modelattah($table.'attach');
	
	if($_POST['msetting']) {
		$_POST['msetting'] = serialize($_POST['msetting']);
	}
		
	$_POST['mtable'] = $table;
	$_POST['uid'] = $_SESSION['uid'];
	$_POST['username'] = $_SESSION['username'];
	$_POST['dateline'] = TIMESTAMP;
	
	return DB::insert('model', $_POST);

}

function create_modeltable($table) {

	DB::query("
		CREATE TABLE  `".DB::table($table)."` (
		 `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT,
		 `catid` int(10) NOT NULL default '0',
		 `pic` varchar(255) NOT NULL,
		 `remote` tinyint(1) UNSIGNED NOT NULL default '0',
		 `dateline` INT( 10 ) UNSIGNED NOT NULL DEFAULT  '0',
		  PRIMARY KEY (  `id` )
		) ENGINE = MYISAM ;
	");
	
}

function create_modelcat($table) {
	
	DB::query("
		CREATE TABLE `".DB::table($table)."` (
		  `catid` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
		  `upid` mediumint(8) UNSIGNED NOT NULL default '0',
		  `catname` varchar(255) NOT NULL default '',
		  `datarows` int(10) UNSIGNED NOT NULL default '0',
		  `displayorder` smallint(6) NOT NULL default '0',
		  `dateline` int(10) UNSIGNED NOT NULL default '0',
		   PRIMARY KEY  (`catid`),
		  KEY `displayorder` (`displayorder`)
		) ENGINE=MyISAM ;
	");
	
}

function create_modelattah($table) {
	
	DB::query("
		CREATE TABLE `".DB::table($table)."` (
		  `attachid` mediumint(8) unsigned NOT NULL auto_increment,
		  `dataid` mediumint(8) unsigned NOT NULL default '0',
		  `uid` mediumint(8) unsigned NOT NULL default '0',
		  `filename` varchar(255) NOT NULL default '',
		  `filetype` varchar(255) NOT NULL default '',
		  `filesize` int(10) unsigned NOT NULL default '0',
		  `attachment` varchar(255) NOT NULL default '',
		  `isimage` tinyint(1) NOT NULL default '0',
		  `thumb` tinyint(1) unsigned NOT NULL default '0',
		  `remote` tinyint(1) unsigned NOT NULL default '0',
		  `dateline` int(10) unsigned NOT NULL default '0',
		   PRIMARY KEY  (`attachid`),
		   KEY `dataid` (`dataid`)
		) ENGINE=MyISAM ;
	");
}

function delete_model($mids) {
	
	if(is_array($mids)) {
		$mids = dimplode($mids);
	}
	
	$tables = DB::getCol("SELECT mtable FROM ".DB::table('model')." WHERE mid IN($mids)");
	
	foreach($tables as $table) {
		drop_table($table.'cat');
		drop_table($table.'attach');
		drop_table($table);
		DB::delete('model', "mtable='$table'");
	}
	
}

function drop_table($table) {
	if(DB::table_exists($table)) {
		DB::query('DROP TABLE '.DB::table($table));
	}
}

function add_modelfield($mid) {

	include_once libfile('function/pinyin');
	
	$model = DB::getRow("SELECT mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	$tablename = $model['mtable'];
	
	if(!$tablename) {
		cpmsg('modeltable_is_empty', 'action=model&operation=fieldlist', 'error');
	}
	
	$fieldname = pinyin($_POST['name']);
	$fieldtype = fieldtype($_POST['type']);

	add_tablefield($tablename, $fieldname, $fieldtype);
	
	$data = array(
		'name' => $_POST['name'],
		'type' => $_POST['type'],
		'extra' => explode("\r\n",$_POST['extra']),
		'value' => $_POST['value'],
		'displayorder' => intval($_POST['displayorder']),
		'tip' => $_POST['tip'],
		'required' => intval($_POST['required']),
		'status' => $_POST['status']
	);
	
	$mdata = $model['mdata'];
	$mdata = unserialize($mdata);

	$mdata[$fieldname] = $data;
	$mdata = serialize($mdata);
	
	return DB::update('model', array('mdata'=>$mdata), "mid='$mid'");
	
}

function edit_modelfield($mid, $fieldname) {
	
	include_once libfile('function/pinyin');
	
	$model = DB::getRow("SELECT mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	$tablename = $model['mtable'];
	
	if(!$tablename) {
		cpmsg('modeltable_is_empty', 'action=model&operation=fieldlist', 'error');
	}
	
	$postname = pinyin($_POST['name']);
	$fieldtype = fieldtype($_POST['type']);
	
	edit_tablefield($tablename, $fieldname, $postname, $fieldtype);
	
	$data = array(
		'name' => $_POST['name'],
		'type' => $_POST['type'],
		'extra' => explode("\r\n",$_POST['extra']),
		'value' => $_POST['value'],
		'displayorder' => intval($_POST['displayorder']),
		'tip' => $_POST['tip'],
		'required' => intval($_POST['required']),
		'status' => intval($_POST['status'])
	);
	
	$mdata = $model['mdata'];
	$mdata = unserialize($mdata);

	if($fieldname == $postname) {
		$mdata[$fieldname] = $data;
	} else {
		unset($mdata[$fieldname]);
		$mdata[$postname] = $data;
	}
	
	$mdata = serialize($mdata);
	
	return DB::update('model', array('mdata'=>$mdata), "mid='$mid'");
}

function delete_modelfield($mid, $fields) {
	
	$model = DB::getRow("SELECT mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	$tablename = $model['mtable'];
	
	if(!$tablename) {
		cpmsg('modeltable_is_empty', 'action=model&operation=fieldlist', 'error');
	}
	
	$mdata = $model['mdata'];
	$mdata = unserialize($mdata);
	
	foreach($fields as $field) {

		delete_tablefield($tablename, $field);
		unset($mdata[$field]);
	}
	
	$mdata = serialize($mdata);
	return DB::update('model', array('mdata'=>$mdata), "mid='$mid'");
}

function fieldtype($htmltype) {
	
	$fieldtype = '';
	$filedlenth = max(intval($_POST['length']),1);
	
	switch($htmltype) {
		case 'input':
		case 'checkbox':
		case 'radio':
		case 'calendar':
			$fieldtype = " VARCHAR( $filedlenth ) ";
			break;
		case 'number':
			$fieldtype = " INT( $filedlenth ) ";
			break;	
		case 'textarea':
			$fieldtype = ' TEXT ';
			$_POST['extra'] = '';
			break;
		case 'fulltextarea':
			$fieldtype = ' MEDIUMTEXT ';
			$_POST['extra'] = '';
			break;
		default:
			$fieldtype = " VARCHAR( $filedlenth ) ";
	}

	return $fieldtype;
}

function add_tablefield($tablename, $fieldname, $fieldtype) {
	
	if(DB::field_exists($tablename, $fieldname)) {
		cpmsg('fieldname_is_empty', 'action=model&operation=fieldlist&mid='.$_GET['mid'], 'error');
	}	
	
	DB::query("
		ALTER TABLE  `".DB::table($tablename)."` 
		ADD  `$fieldname` $fieldtype NOT NULL ;
	");
	
}

function edit_tablefield($tablename, $fieldname, $postname, $fieldtype) {
	
	if(!DB::field_exists($tablename, $fieldname)) {
		cpmsg('field_not_found', 'action=model&operation=fieldlist&mid='.$_GET['mid'], 'error');
	}
	
	DB::query("
		ALTER TABLE `".DB::table($tablename)."` 
		CHANGE `$fieldname` `$postname` $fieldtype 
	");
	
}

function delete_tablefield($tablename, $fieldname) {
	
	if(!DB::field_exists($tablename, $fieldname)) {
		return;
	}
	
	DB::query("
		ALTER TABLE  `".DB::table($tablename)."` 
		DROP  `$fieldname`
	");
	
}

function delete_data($mtable, $dataid) {
	
	if(is_array($dataid)) {
		$dataid = dimplode($dataid);
	}
	
	$catids = DB::getCol("SELECT catid FROM ".DB::table($mtable)." WHERE id IN($dataid)");

	$status = DB::delete($mtable, "id IN($dataid)");
	update_datarows($mtable, $catids);
	delete_dataattach($mtable.'attach', $dataid);
	return $status;
}

function delete_dataattach($table, $dataid) {
	
	if(is_array($dataid)) {
		$dataid = dimplode($dataid);
	}
	
	$query = DB::query("SELECT * FROM ".DB::table($table)." WHERE dataid IN ($dataid)");
	while ($value = DB::fetch($query)) {
		$attachment[] = $value;
		$attachdel[] = $value['attachid'];
	}
	
	foreach ($attachment as $value) {
		delete_attach($value['attachment'], 'model', $value['thumb'], $value['remote']);
	}
	DB::delete($table, "dataid IN ($dataid)");
}

function update_datarows($table, $catids) {
	foreach($catids as $catid) {
		$count = DB::getOne('SELECT COUNT(*) FROM '.DB::table($table)." WHERE catid='$catid'");
		DB::query('UPDATE '.DB::table($table.'cat')." SET datarows='{$count}' WHERE catid='$catid'");
	}
}

function delete_dataacat($mtable, $catid) {
	
	if(is_array($catid)) {
		$catid = dimplode($catid);
	}
	
	$dataids = DB::getCol("SELECT id FROM ".DB::table($mtable)." WHERE catid IN($catid)");
	delete_data($mtable, $dataids);
	DB::delete($mtable.'cat', "catid IN({$catid})");
	updatecache('modeldatacat');
}

function htmltypelable($type) {
	$htmltype = cplang('htmltype');
	return $htmltype[$type];
}

function htmltypeselect($type='input') {
	$htmltype = cplang('htmltype');
	$htmltypeselect = '<select id="type" name="type" onchange="showextra(this.selectedIndex);">';
	
	foreach($htmltype as $key => $value) {
		
		$htmltypeselect .= '<option value="'.$key.'" '.($type==$key?'selected=""':'').'>'.$value.'</option>';
	}
	
	$htmltypeselect .= '</select>';
	return $htmltypeselect;
}

function model_menuextra($mid) {
	$str = '<li id="model_'.$mid.'"><a href="'.ADMINSCRIPT.'?action=model&operation=datalist&mid='.$mid.'" hidefocus="true" target="main" class=""><em onclick="menuNewwin(this)" title=""></em>'.$_POST['mname'].cplang('board').'</a></li>';
	$extra = '<script>';
	$extra .= 'if(parent.$(\'menu_model\') != null) parent.$(\'menu_model\').innerHTML+=\''.$str.'\'; parent.initCpMenus(\'leftmenu\');parent.initCpMap();';
	$extra .= '</script>';
	return $extra;
}

function model_menudelete($mid) {
	static $extra = '';
	if(is_array($mid)) {
		foreach($mid as $value) {
			$extra .= model_menudelete($value);
		}
	}
	$extra .= '<script>';
	$extra .= 'if(parent.$(\'model_'.$mid.'\') != null) parent.$(\'model_'.$mid.'\').parentNode.removeChild(parent.$(\'model_'.$mid.'\'));';
	$extra .= '</script>';
	return $extra;
}

function model_menuupdate($mid) {
	$extra = model_menudelete($mid);
	$extra .=model_menuextra($mid);
	return $extra;
}

function showmodeldatacatrow($key, $level = 0, $last = '', $mid, $cattable) {
	global $_G;

	$value = $_G['cache']['modeldatacat'.$mid][$key];
	$return = '';

	$value['datarows'] = DB::getOne('SELECT datarows FROM '.DB::table($cattable)." WHERE catid='$key'");

	if($level == 2) {
		$class = $last ? 'lastchildboard' : 'childboard';
		$return = '<tr class="hover" id="cat'.$value['catid'].'"><td>&nbsp;</td><td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="'.$class.'">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'</div>'.
		'</td><td>'.$value['datarows'].'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatedit&mid='.$mid.'&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatdelete&mid='.$mid.'&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr>';
	} elseif($level == 1) {
		$return = '<tr class="hover" id="cat'.$value['catid'].'"><td>&nbsp;</td><td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="board">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'<a class="addchildboard" href="'.ADMINSCRIPT.'?action=model&operation=datacatadd&mid='.$mid.'&upid='.$value['catid'].'">'.cplang('articlecat_addthirdcat').'</a></div>'.
		'</td><td>'.$value['datarows'].'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatedit&mid='.$mid.'&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatdelete&mid='.$mid.'&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr>';
		for($i=0,$L=count($value['children']); $i<$L; $i++) {
			$return .= showmodeldatacatrow($value['children'][$i], 2, $i==$L-1, $mid, $cattable);
		}
	} else {
		$childrennum = count($_G['cache']['modeldatacat'.$mid][$key]['children']);
		$toggle = $childrennum > 25 ? ' style="display:none"' : '';
		$return = '<tbody><tr class="hover" id="cat'.$value['catid'].'"><td onclick="toggle_group(\'group_'.$value['catid'].'\')"><a id="a_group_'.$value['catid'].'" href="javascript:;">'.($toggle ? '[+]' : '[-]').'</a></td>'
		.'<td class="td25"><input type="text" class="txt" name="neworder['.$value['catid'].']" value="'.$value['displayorder'].'" /></td><td><div class="parentboard">'.
		'<input type="text" class="txt" name="name['.$value['catid'].']" value="'.$value['catname'].'" />'.
		'</div>'.
		'</td><td>'.$value['datarows'].'</td>'.
		'<td>
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatedit&mid='.$mid.'&catid='.$value['catid'].'">'.cplang('edit').'</a>&nbsp;
		<a href="'.ADMINSCRIPT.'?action=model&operation=datacatdelete&mid='.$mid.'&catid='.$value['catid'].'" onclick="return window.confirm(\''.cplang('confirm_delete').'\')">'.cplang('delete').'</a></td></tr></tbody>
		<tbody id="group_'.$value['catid'].'"'.$toggle.'>';
		for($i=0,$L=count($value['children']); $i<$L; $i++) {
			$return .= showmodeldatacatrow($value['children'][$i], 1, '', $mid, $cattable);
		}
		$return .= '</tdoby><tr><td>&nbsp;</td><td colspan="9"><div class="lastboard"><a class="addtr" href="'.ADMINSCRIPT.'?action=model&operation=datacatadd&mid='.$mid.'&upid='.$value['catid'].'">'.cplang('articlecat_addsubcat').'</a></td></div>';
	}
	return $return;
}

function make_htmlformrow($mdata, $content) {
	$htmlformrow = '';
	
	foreach($mdata as $key => $field) {
		
		if($field['type'] == 'input' || $field['type'] == 'number') {
			
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder"><td class="vtop rowform">
				<input type="text" class="txt" name="'.$key.'" value="'.($_GET['id']&&$content[$key]?$content[$key]:$field['value']).'">
				</td><td class="vtop tips2" s="1">'.$field['tip'].'</td></tr>
			';
			
		} elseif($field['type'] == 'textarea') {
			
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder">
				<td class="vtop rowform">
				<textarea rows="6" ondblclick="textareasize(this, 1)" onkeyup="textareasize(this, 0)" name="'.$key.'" cols="50" class="tarea">'.($_GET['id']&&$content[$key]?$content[$key]:$field['value']).'</textarea>
				</td><td class="vtop tips2" s="1">'.$field['tip'].'</td></tr>
			';
			
		} elseif($field['type'] == 'checkbox') {
		
			$content[$key] = unserialize($content[$key]);			
			$htmloption = '';
			foreach($field['extra'] as $k => $option) {
				if(!$option) continue;
				$checkd = in_array($k, $content[$key]) ? 'checked' : '';
				$htmloption .= '<li class="'.$checkd.'"><input class="checkbox" type="checkbox" name="'.$key.'[]" value="'.$k.'" '.$checkd.'>&nbsp;'.$option.'</li>';
			}
		
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder">
				<td class="vtop rowform">
				<ul onmouseover="altStyle(this);">'.$htmloption.'</ul>
				</td><td class="vtop tips2" s="1">'.$field['tip'].'</td></tr>
			';
			
		} elseif($field['type'] == 'radio') {
			
			$htmloption = '';
			$defaultchecked = empty($_GET['id']) ? 'checked=""' : '';
			foreach($field['extra'] as $k => $option) {
				if(!$option) continue;
				$checked = ($defaultchecked || $content[$key]==$k?'checked=""':'') ? 'checked' : '';
				
				$htmloption .= '<li class="'.$checked.'"><input class="radio" type="radio" name="'.$key.'" value="'.$k.'" '.$checked.'>&nbsp;'.$option.'</li>';
				$defaultchecked = '';
			}
			
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder">
				<td class="vtop rowform">
				<ul onmouseover="altStyle(this);">'.$htmloption.'</ul>
				</td><td class="vtop tips2" s="1">'.$field['tip'].'</td></tr>
			';
		} elseif($field['type'] == 'fulltextarea') {
			
			include libfile('class/editor');
			$editor = editor::instance();
			
			$htmltextarea = $editor->create($key, ($_GET['id']&&$content[$key]?$content[$key]:$field['value']));
			
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder">
				<td colspan="2" class="vtop rowform">'.$htmltextarea.'</td><!--td class="vtop tips2" s="1"></td--></tr>
			';
			
		} elseif($field['type'] == 'calendar') {
			
			$htmlformrow .= '
				<tr><td colspan="2" class="td27" s="1">'.$field['name'].':</td></tr>
				<tr class="noborder"><td class="vtop rowform">
				<input type="text" class="txt" name="'.$key.'" value="'.($_GET['id']&&$content[$key]?$content[$key]:dgmdate(TIMESTAMP)).'" onclick="showcalendar(event, this, 1)">
				<script type="text/javascript" src="static/js/calendar.js"></script>
				</td><td class="vtop tips2" s="1">'.$field['tip'].'</td></tr>
			';
			
		}
		
	}
	
	return $htmlformrow;
	
}

function datacat_select($name='catid', $shownull=true, $current='') {
	global $_G;

	loadcache('modeldatacat'.$_GET['mid']);
	$category = $_G['cache']['modeldatacat'.$_GET['mid']];

	$select = "<select id=\"$name\" name=\"$name\" class=\"ps vm\">";
	if($shownull) {
		$select .= '<option value="">'.lang('select_category').'</option>';
	}
	foreach ($category as $value) {
		if($value['level'] == 0) {
			$selected = ($current && $current==$value['catid']) ? 'selected="selected"' : '';
			$select .= "<option value=\"$value[catid]\"$selected>$value[catname]</option>";
			if(!$value['children']) {
				continue;
			}
			foreach ($value['children'] as $catid) {
				$selected = ($current && $current==$catid) ? 'selected="selected"' : '';
				$select .= "<option value=\"{$category[$catid][catid]}\"$selected>-- {$category[$catid][catname]}</option>";
				if($category[$catid]['children']) {
					foreach ($category[$catid]['children'] as $catid2) {
						$selected = ($current && $current==$catid2) ? 'selected="selected"' : '';
						$select .= "<option value=\"{$category[$catid2][catid]}\"$selected>---- {$category[$catid2][catname]}</option>";
					}
				}
			}
		}
	}
	$select .= "</select>";
	return $select;
}

function checkrequired($mdata) {
	$result = array('status' => 1);
	foreach($mdata as $key => $field) {
		if($field['required'] && !$_POST[$key]){
			$result['status'] = 0;
			$result['name'] = $field['name'];
			return $result;
		}
	}
	return $result;
}

function model_upload_show($attach, $type='model', $mid) {

	$imagehtml = $filehtml = $coverstr ='';

	if($attach['isimage']) {
		$imagehtml = get_modeluploadcontent($attach, $type, $mid);
		$coverstr = addslashes(serialize(array('pic'=>$type.'/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote'])));
	} else {
		$filehtml = get_modeluploadcontent($attach, $type, $mid);
	}

	echo '<script>';
	if($imagehtml) echo 'parent.$(\'attach_image_body\').innerHTML = \''.addslashes($imagehtml).'\'+parent.$(\'attach_image_body\').innerHTML;';
	if($filehtml) echo 'parent.$(\'attach_file_body\').innerHTML = \''.addslashes($filehtml).'\'+parent.$(\'attach_file_body\').innerHTML;';
	echo 'if(parent.$(\'localfile_'.$_GET['attach_target_id'].'\') != null)parent.$(\'localfile_'.$_GET['attach_target_id'].'\').style.display = \'none\';';
	echo 'parent.$(\'attach_ids\').value += \','.$attach['attachid'].'\';';
	if($coverstr) echo 'if(parent.$(\'conver\').value == \'\')parent.$(\'conver\').value = \''.$coverstr.'\';';
	echo '</script>';

}

function get_modeluploadcontent($attach, $type='model', $mid) {

	$return = '';
	$dotype = $dotype ? 'checked' : '';
	if($attach['isimage']) {
		$pic = attachurl($attach['attachment'], $type, $attach['thumb'], $attach['remote']);		
		$small_pic = $attach['thumb'] ? getimgthumbname($pic) : '';
		$check = $attach['pic'] == $type.'/'.$attach['attachment'] ? 'checked' : '';
		
		$return .= '<table id="attach_list_'.$attach['attachid'].'" width="200" height="80" style="float:left;">';
		$return .= '<td><a href="'.$pic.'" target="_blank"><img src="'.($small_pic ? $small_pic : $pic).'" width="40" height="40"></a></td>';
		$return .= '<td>';
		$return .= '<label for="setconver'.$attach['attachid'].'"><input type="radio" name="setconver" id="setconver'.$attach['attachid'].'" class="pr" value="1" onclick=setConver(\''.addslashes(serialize(array('pic'=>$type.'/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote']))).'\') '.$check.'>'.lang('core', 'set_to_conver').'</label><br>';
		if($small_pic) $return .= '<a href="javascript:void(0);" onclick="insertImage(\''.$small_pic.'\', \''.$pic.'\');return false;">'.lang('core', 'insert_small_image').'</a><br>';
		$return .= '<a href="javascript:void(0);" onclick="insertImage(\''.$pic.'\');return false;">'.lang('core', 'insert_large_image').'</a><br>';
		$return .= '<a href="javascript:void(0);" onclick="deleteAttach(\''.$attach['attachid'].'\', \''.ADMINSCRIPT.'?action=upload&operation='.$type.'_attach_delete&mid='.$mid.'&id='.$attach['attachid'].'\');return false;">'.lang('core', 'delete').'</a>';
		$return .= '</td>';
		$return .= '</table>';

	} else {
		$return .= '<table id="attach_list_'.$attach['attachid'].'" width="200" height="80" style="float:left; margin-right:20px;">';
		$return .= '<td><a href="misc.php?mod=attachment&type=model&mid='.$mid.'&id='.$attach['attachid'].'" target="_blank">'.$attach['filename'].'</a></td>';
		$return .= '<td>';
		$return .= '<a href="javascript:void(0);" onclick="deleteAttach(\''.$attach['attachid'].'\', \''.ADMINSCRIPT.'?action=upload&operation='.$type.'_attach_delete&mid='.$mid.'&id='.$attach['attachid'].'&op=delete\');return false;">'.lang('core', 'delete').'</a>';
		$return .= '</td>';
		$return .= '</table>';
	}

	return $return;

}



?>